package com.cskaoyan.controller;

import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.baseresultVO.BaseRespVo;
import com.cskaoyan.bean.domain.Role;
import com.cskaoyan.bean.roleBeans.PermissionResultVo;
import com.cskaoyan.bean.roleBeans.PermissionVo;
import com.cskaoyan.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @User: Yr
 * @Date: 2021/12/1 10:36
 * @Description: 系统管理-角色管理模块
 */

@RestController
@RequestMapping("admin/role")
public class RoleController {


    @Autowired
    RoleService roleService;

    /**
     * 按条件查询所有role的业务逻辑
     * @Description 仅显示deleted为false的
     * @param name
     * @param baseParam (page limit sort 和order)
     * @return BaseRespVo (data域为roleList及分页信息)
     */
    @RequiresPermissions("admin:role:list")
    @RequestMapping("list")
    public BaseRespVo list(String name, BaseParam baseParam) {
        BaseData data = roleService.list(name, baseParam);
        return BaseRespVo.ok(data);
    }


    /**
     * 逻辑删除role的业务逻辑
     * @Description 不能删除id为1的role
     * @param role 参数接收Json数据role
     * @return BaseRespVo
     */
    @RequiresPermissions("admin:role:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Role role) {
        if(role.getId() == 1)return BaseRespVo.error(401,"不能删除超级管理员角色");
        roleService.delete(role);
        return BaseRespVo.ok(null);
    }

    /**
     * 新增role的业务逻辑
     * @Description 校验重名 (若与已被逻辑删除的角色重名,提示系统内部错误)
     * @param role 参数接收json数据role(name和desc)
     * @return
     */
    @RequiresPermissions("admin:role:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Role role) {
        Role resultRole = roleService.create(role);
        if (resultRole == null) return BaseRespVo.error(640, "角色已存在");
        return BaseRespVo.ok(resultRole);
    }

    /**
     * 修改role的业务逻辑
     *
     * @param role 参数接收json数据role
     * @return
     */
    @RequiresPermissions("admin:role:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Role role) {
        //校验重名  其他不校验
        int affectedRows = roleService.update(role);
        if (affectedRows == 0) return BaseRespVo.error(640, "角色已存在");
        return BaseRespVo.ok(null);
    }


    /**
     * options请求的业务逻辑
     *
     * @return
     */
    @RequestMapping("options")
    public BaseRespVo options() {
        BaseData data = roleService.options();
        return BaseRespVo.ok(data);
    }

    /**
     * 根据roleId返回已分配的权限数据
     *
     * @param roleId
     * @return
     */
    @RequiresPermissions("admin:role:permission:get")
    @GetMapping("permissions")
    public BaseRespVo permissions(Integer roleId) {
        PermissionResultVo data = roleService.permissionsByGet(roleId);
        return BaseRespVo.ok(data);
    }

    /**
     * 给指定roleId分配新权限
     *
     * @return
     */
    @RequiresPermissions("admin:role:permission:update")
    @PostMapping("permissions")
    public BaseRespVo permissions(@RequestBody Map map) {
        Integer roleId = (Integer) map.get("roleId");
        if (roleId == 1)return BaseRespVo.error(641,"当前角色超级权限不能变更");
        ArrayList<String> permissions = (ArrayList<String>) map.get("permissions");
        roleService.permissionsByPost(roleId,permissions);
        return BaseRespVo.ok(null);
    }


    /**
     * 获取到权限管理信息插入到数据库
     *
     * @param permission
     */
    @RequestMapping("getAuthData")
    public void getAuthData(@RequestBody List<PermissionVo> list) {

        roleService.getAuthData(list);

    }


}
